$(function () {
    obj.init();
});

var obj = new function () {
    return {
        dataGrid: null,
        init: function () {
            this.dataGrid = new xhDataGrid($('#list'), 'res/getCheckTypeInList');
            this.dataGrid.parseToolbar().init().activeButton([], ['bt_list_addRemark', 'bt_list_addCheckMsg']);
            xhExtra.clearLoadSuccess(this.dataGrid);
            this.listener();
        },
        listener: function () {
            var $this = this;
            //查询
            xhTool.bindDocKeyDown(
                $('#bt_list_search').click(function () {
                    $this.funObj().searchList();
                })
            );
            //双击
            $this.dataGrid.onDblClickRow = function(rowIndex, rowData){
                $this.funObj().checkHistory(rowData['iApplyId'], rowData['sBookName']);
            };
            $('#bt_list_addRemark').click(function () {
                if(xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().addRemark();
            });
            $('#bt_list_addCheckMsg').click(function () {
                if(xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().addCheckMsg();
            });
            $('#bt_list_checkHistory').click(function () {
                if(xhExtra.buttonDisabled($(this))) return false;
                var iApplyId = $this.dataGrid.getSelectValue('iApplyId');
                var sBookName = $this.dataGrid.getSelectValue('sBookName');
                $this.funObj().checkHistory(iApplyId, sBookName);
            })
        },
        funObj: function () {
            var $this = this;
            return {
                searchList: function () {
                    $this.dataGrid.reload({
                        search: {
                            vague: xhExtra.getValue($('#list_vague'))
                        },
                        status: xhExtra.getValue($('#list_status')),
                        checkMsg: xhExtra.getValue($('#list_checkMsg'))
                    })
                },
                addRemark: function () {
                    var iId = $this.dataGrid.getSelectValue('iId');
                    var createId = $this.dataGrid.getSelectValue('iCreateUserId');
                    var nowUserId = $this.dataGrid.getSelectValue('nowUserId');
                    if (!iId) {
                        window.top.xhTop.showError('请选择后操作');
                        return false;
                    }
                    if (createId != nowUserId) {
                        window.top.xhTop.showError('仅确认老师可添加备注');
                        return false;
                    }
                    window.top.swal({
                        showCancelButton: true,
                        confirmButtonText: '确认',
                        cancelButtonText: '取消',
                        showLoaderOnConfirm: true,
                        allowOutsideClick: false,
                        title: '填写备注',
                        html: '<div><textarea id="remark" class="swal2-textarea" placeholder="必填">',
                        preConfirm: function() {
                            return new Promise(function (resolve, reject) {
                                var remark     = $('#remark', window.top.document).val();
                                if (remark < 1){
                                    reject('备注不能为空');
                                } else {
                                    xhExtra.ajaxRequest('res/setCheckTypeInRemark', {
                                        id: iId,
                                        remark: remark
                                    }, {success: true}, function (_data) {
                                        if (_data.code === 200) {
                                            location.reload();
                                        }
                                    });
                                    resolve();
                                }
                            })
                        }
                    })
                },
                addCheckMsg: function () {
                    var iId = $this.dataGrid.getSelectValue('iId');
                    var createId = $this.dataGrid.getSelectValue('iCreateUserId');
                    var nowUserId = $this.dataGrid.getSelectValue('nowUserId');
                    if (!iId) {
                        window.top.xhTop.showError('请选择后操作');
                        return false;
                    }
                    if (createId != nowUserId) {
                        window.top.xhTop.showError('仅确认老师可添加确认信息');
                        return false;
                    }
                    window.top.swal({
                        showCancelButton: true,
                        confirmButtonText: '确认',
                        cancelButtonText: '取消',
                        showLoaderOnConfirm: true,
                        allowOutsideClick: false,
                        title: '填写确认信息',
                        html: '<div><textarea id="checkMsg" class="swal2-textarea" placeholder="必填">',
                        preConfirm: function() {
                            return new Promise(function (resolve, reject) {
                                var checkMsg     = $('#checkMsg', window.top.document).val();
                                if (checkMsg < 1){
                                    reject('确认信息不能为空');
                                } else {
                                    xhExtra.ajaxRequest('res/setCheckMsg', {
                                        id: iId,
                                        checkMsg: checkMsg
                                    }, {success: true}, function (_data) {
                                        if (_data.code === 200) {
                                            location.reload();
                                        }
                                    });
                                    resolve();
                                }
                            })
                        }
                    })
                },
                checkHistory: function (_id, _bookName) {
                    window.top.xhTop.showModal({
                        title: '确认记录：' + _bookName,
                        content: xhTool.getFrameString('res/checkTypeInHistory', {id: _id}, 650),
                        size: 'md',
                        width: '800',
                        backClose: false
                    });
                }
            }
        }
    }
};